home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / slideshow / sources (complete) / slideshow.java < prev   
Encoding:
Java Source  |  2000-06-23  |  13.8 KB  |  539 lines

  1. import java.awt.*;
  2. import java.awt.event.ActionListener;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.KeyEvent;
  5. import java.io.File;
  6. import java.util.Vector;
  7.  
  8. import com.apple.mrj.MRJApplicationUtils;
  9. import com.apple.mrj.MRJOpenDocumentHandler;
  10. import com.apple.mrj.MRJQuitHandler;
  11. import com.apple.mrj.MRJAboutHandler;
  12.  
  13. public class SlideShow extends Frame
  14. {
  15.     //Declare and define constants
  16.     //Insert "SlideShow Constants"
  17.     protected static final int SLEEP_DELAY = 1000;
  18.     protected static final int WIDTH = 430;
  19.     protected static final int HEIGHT = 270;
  20.  
  21.     //Declare data members
  22.     //Insert "SlideShow data members"
  23.     protected Vector files;
  24.     protected Image currentImage;
  25.     protected int currentIndex;
  26.     protected boolean isFullScreen;
  27.     protected boolean isPlaying;
  28.     protected PlayRunnable playRunnable;
  29.     protected Thread thread;
  30.     protected FileDialog openFileDialog1;
  31.     protected Controller controls;
  32.     protected AboutDialog aboutDialog1;
  33.  
  34.     //DECLARE_MENUS
  35.     //Declare Menus, Menu Items and the Menu Bar
  36.     //Insert "SlideShow declare menus"
  37.     protected MenuBar menuBar1;
  38.     protected Menu fileMenu;
  39.     protected MenuItem openMenuItem;
  40.     protected MenuItem quitMenuItem;
  41.     protected Menu slideShowMenu;
  42.     protected MenuItem playMenuItem;
  43.     protected MenuItem backMenuItem;
  44.     protected MenuItem forwardMenuItem;
  45.     protected Menu optionsMenu;
  46.     protected MenuItem controlsMenuItem;
  47.     protected MenuItem fullScreenMenuItem;
  48.     
  49.     /**
  50.      * The entry point to our application
  51.      */
  52.     static public void main(String args[])
  53.     {
  54.         //Instantiate our SlideShow, make it visible, and register our MRJ handlers.
  55.         //Insert "SlideShow main"
  56.         SlideShow slideShow = new SlideShow();
  57.         slideShow.setVisible(true);
  58.         slideShow.registerMRJHandlers();
  59.     }
  60.  
  61.     public SlideShow()
  62.     {    
  63.         //INIT_STATE
  64.         //Initialize state information
  65.         //Insert "SlideShow init state"
  66.         isFullScreen = false;
  67.         isPlaying = false;
  68.         files = new Vector();
  69.         currentImage = null;
  70.         currentIndex = -1;
  71.         
  72.         //INIT_CONTROLS
  73.         //Setup and configure our SlideShow
  74.         //Insert "SlideShow init controls"
  75.         setLayout(null);
  76.         setVisible(false);
  77.         setSize(WIDTH, HEIGHT);
  78.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  79.         setLocation((screenSize.width - WIDTH) / 2, (screenSize.height - HEIGHT) / 2);
  80.         setBackground(Color.black);
  81.         openFileDialog1 = new FileDialog(this);
  82.         openFileDialog1.setMode(FileDialog.LOAD);
  83.         openFileDialog1.setTitle("Open");
  84.         openFileDialog1.setFilenameFilter(new ImageNameFilter());
  85.         setTitle("SlideShow");
  86.         controls = new Controller(this);
  87.  
  88.         //INIT_MENUS
  89.         //Create, configure, and setup the menubar, menus, and menu items.
  90.         //Insert "SlideShow init menus"
  91.         menuBar1 = new MenuBar();
  92.         
  93.         //File menu
  94.         fileMenu = new Menu("File");
  95.         openMenuItem = new MenuItem("Open...");
  96.         openMenuItem.setShortcut(new MenuShortcut(KeyEvent.VK_O,false));
  97.         fileMenu.add(openMenuItem);
  98.         fileMenu.addSeparator();
  99.         quitMenuItem = new MenuItem("Quit");
  100.         quitMenuItem.setShortcut(new MenuShortcut(KeyEvent.VK_Q,false));
  101.         fileMenu.add(quitMenuItem);
  102.         menuBar1.add(fileMenu);
  103.         
  104.         //SlideShow menu
  105.         slideShowMenu = new Menu("SlideShow");
  106.         playMenuItem = new MenuItem("Toggle Play");
  107.         playMenuItem.setShortcut(new MenuShortcut(KeyEvent.VK_P,false));
  108.         slideShowMenu.add(playMenuItem);
  109.         backMenuItem = new MenuItem("Back");
  110.         backMenuItem.setShortcut(new MenuShortcut(KeyEvent.VK_OPEN_BRACKET,false));
  111.         slideShowMenu.add(backMenuItem);
  112.         forwardMenuItem = new MenuItem("Forward");
  113.         forwardMenuItem.setShortcut(new MenuShortcut(KeyEvent.VK_CLOSE_BRACKET,false));
  114.         slideShowMenu.add(forwardMenuItem);
  115.         menuBar1.add(slideShowMenu);
  116.         
  117.         //Options menu
  118.         optionsMenu = new Menu("Options");
  119.         controlsMenuItem = new MenuItem("Toggle Controls");
  120.         optionsMenu.add(controlsMenuItem);
  121.         fullScreenMenuItem = new MenuItem("Toggle Full Screen");
  122.         optionsMenu.add(fullScreenMenuItem);
  123.         menuBar1.add(optionsMenu);
  124.         setMenuBar(menuBar1);
  125.         
  126.         //REGISTER_LISTENERS
  127.         //Register ActionListeners with the menu items and the controller.
  128.         //Insert "SlideShow register listeners"
  129.         Action aAction = new Action();
  130.         openMenuItem.addActionListener(aAction);
  131.         quitMenuItem.addActionListener(aAction);
  132.         controlsMenuItem.addActionListener(aAction);
  133.         fullScreenMenuItem.addActionListener(aAction);
  134.         playMenuItem.addActionListener(aAction);
  135.         backMenuItem.addActionListener(aAction);
  136.         forwardMenuItem.addActionListener(aAction);
  137.         controls.addActionListener(aAction);
  138.     }
  139.  
  140.     /**
  141.      * Starts or stops cycling forward through the list of image files to display.
  142.      */
  143.     public void togglePlaying()
  144.     {
  145.         //Handle starting and stopping the automatic progression of the show.
  146.         //Insert "SlideShow togglePlaying"
  147.         if (isPlaying)
  148.         {
  149.             if (playRunnable != null)
  150.                 playRunnable.isRun = false;
  151.             isPlaying = false;
  152.         }
  153.         else
  154.         {
  155.             if (thread == null || !thread.isAlive())
  156.             {
  157.                 if (playRunnable != null)
  158.                     playRunnable.isRun = false;
  159.                     
  160.                 playRunnable = new PlayRunnable();
  161.                 thread = new Thread(playRunnable);
  162.                 thread.start();
  163.                 isPlaying = true;
  164.             }
  165.         }
  166.     }
  167.     
  168.     //Inner class to implement our automatic progression of the show.
  169.     //Insert "SlideShow PlayRunnable"
  170.     class PlayRunnable implements Runnable
  171.     {
  172.         public boolean isRun = true;
  173.         
  174.         public void run()
  175.         {
  176.             while (isRun)
  177.             {
  178.                 oneStep(true);
  179.                 
  180.                 try
  181.                 {
  182.                     Thread.sleep(SLEEP_DELAY);
  183.                 }
  184.                 catch (InterruptedException exc) { }
  185.             }
  186.         }
  187.     }
  188.     
  189.     /**
  190.      * Steps the slide show forward or backwards by one image.
  191.      * @param if true, step forward, if false, step backward.
  192.      */
  193.     public void oneStep(boolean isForward)
  194.     {
  195.         //Handle stepping forward or backward in the list of image files,
  196.         //load the image, and repainting.
  197.         //Insert "SlideShow oneStep"
  198.         int size = files.size();
  199.  
  200.         if (size > 0)
  201.         {
  202.             if (isForward)
  203.             {
  204.                 currentIndex++;
  205.                 if (currentIndex >= size)
  206.                     currentIndex = 0;
  207.             }
  208.             else
  209.             {
  210.                 currentIndex--;
  211.                 if (currentIndex < 0)
  212.                     currentIndex = size - 1;
  213.             }
  214.             
  215.             File file = (File)files.elementAt(currentIndex);
  216.             if (file != null)
  217.             {
  218.                 Image image = Misc.loadImage(file.getPath(), this, false);
  219.                 if (image != null)
  220.                 {
  221.                     if (currentImage != null)
  222.                         currentImage.flush();
  223.                     currentImage = image;
  224.                     repaint();
  225.                 }
  226.             }
  227.         }
  228.     }
  229.     
  230.     /**
  231.      * Handles sizing of the window to utilize the full screen size, or to use the size of the image.
  232.      */
  233.     public void toggleFullScreen()
  234.     {
  235.         //Handle toggling the frame window size between the image size, screen size, and full screen.
  236.         //Insert "SlideShow toggleFullScreen"
  237.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  238.         
  239.         if (isFullScreen)
  240.         {
  241.             int width = WIDTH;
  242.             int height = HEIGHT;
  243.  
  244.             if (currentImage != null)
  245.             {
  246.                 width = currentImage.getWidth(this);
  247.                 height = currentImage.getHeight(this);
  248.                 
  249.                 //Make sure the window fits on the screen
  250.                 width = Math.min(width, screenSize.width);
  251.                 height = Math.min(height, screenSize.height);
  252.             }
  253.  
  254.             setLocation((screenSize.width - width) / 2, (screenSize.height - height) / 2);
  255.             setSize(width, height);
  256.  
  257.             isFullScreen = false;
  258.         }
  259.         else
  260.         {
  261.             int top = 21;
  262.             int sides = 5;
  263.             setBounds(-sides, -top, screenSize.width + 2 * sides, screenSize.height + top + sides);
  264.             isFullScreen = true;
  265.         }
  266.     }
  267.  
  268.     /**
  269.      * Shows or hides the control window.
  270.      */
  271.     public void toggleControls()
  272.     {
  273.         //Handle toggling the visibility of the controller
  274.         //Insert "SlideShow toggleControls"
  275.         if (controls != null)
  276.             controls.setVisible(!controls.isVisible());
  277.     }
  278.         
  279.     /**
  280.      * Gets called when the user chooses the Quit menu item, or when the
  281.      * application receives a quit message from the Finder (or other app).
  282.      */
  283.     protected void doOnQuit()
  284.     {
  285.         //Handle cleaning up, and quit.
  286.         //Insert "SlideShow doOnQuit"
  287.         //Do any clean up here.
  288.         
  289.         //Exit with success.
  290.         System.exit(0);
  291.     }
  292.     
  293.     /**
  294.      * Gets called when the user selects the about menu item in the Apple Menu.
  295.      */
  296.     protected void doAbout()
  297.     {
  298.         //Handle displaying about information here
  299.         //Insert "SlideShow doAbout"
  300.         if (aboutDialog1 == null)
  301.             aboutDialog1 = new AboutDialog(this, true);
  302.         aboutDialog1.setVisible(true);
  303.     }
  304.  
  305.     public void paint(Graphics g)
  306.     {
  307.         //Handle scaling and drawing the image to fit in the frame content area.
  308.         //Insert "SlideShow paint"
  309.         if (currentImage != null)
  310.         {
  311.             Dimension s = getSize();
  312.             int iWidth = currentImage.getWidth(this);
  313.             int iHeight = currentImage.getHeight(this);
  314.  
  315.             int scaleWidth = iWidth;
  316.             int scaleHeight = iHeight;
  317.             
  318.             int wDelta = s.width - iWidth;
  319.             int hDelta = s.height - iHeight;
  320.             
  321.             if (wDelta > 0 && hDelta > 0)
  322.             {
  323.                 //The image fits, just draw it.
  324.                 g.drawImage(currentImage, (s.width - iWidth) / 2, (s.height - iHeight) / 2, this);
  325.             }
  326.             else
  327.             {
  328.                 //The image doesn't fit. We need to scale it down to fit.
  329.  
  330.                 float ratio = 1;
  331.                 
  332.                 if (wDelta < hDelta)
  333.                 {
  334.                     if (iWidth > 0)
  335.                     {
  336.                         //width needs to be scaled to fit
  337.                         ratio = s.width / (float)iWidth;
  338.                     }
  339.                 }
  340.                 else
  341.                 {
  342.                     if (iHeight > 0)
  343.                     {
  344.                         //height needs to be scaled to fit
  345.                         ratio = s.height / (float)iHeight;
  346.                     }
  347.                 }
  348.                 
  349.                 scaleWidth = (int)(iWidth * ratio);
  350.                 scaleHeight = (int)(iHeight * ratio);
  351.  
  352.                 g.drawImage(currentImage, (s.width - scaleWidth) / 2, (s.height - scaleHeight) / 2, scaleWidth, scaleHeight, this);
  353.             }
  354.         }
  355.     }
  356.  
  357.     public void setVisible(boolean b)
  358.     {
  359.         //Make sure the controls are visible only when the frame is visible.
  360.         //Insert "SlideShow setVisible"
  361.         super.setVisible(b);
  362.         
  363.         if (controls != null)
  364.             controls.setVisible(b);
  365.     }
  366.     
  367.  
  368.     protected void registerMRJHandlers()
  369.     {
  370.         //Register MRJ handlers for open, about and quit.
  371.         //Insert "SlideShow registerMRJHandlers"
  372.         MRJI IMRJI = new MRJI();
  373.         MRJApplicationUtils.registerOpenDocumentHandler(IMRJI);
  374.         MRJApplicationUtils.registerQuitHandler(IMRJI);
  375.         MRJApplicationUtils.registerAboutHandler(IMRJI);
  376.     }
  377.  
  378.     //Inner class defining the MRJ Interface
  379.     //Insert "SlideShow MRJI"
  380.     class MRJI implements MRJOpenDocumentHandler, MRJQuitHandler, MRJAboutHandler
  381.     {
  382.         
  383.         /**
  384.          * This gets called by MRJ for each file/folder dropped onto the application.
  385.          * If the file is a directory, this recurses through the directory structure
  386.          * collecting image files.
  387.          * @param the file to add to the list of image files to display, or the File
  388.          * object to recurse to look for image files to add to the list.
  389.          */
  390.         public void handleOpenFile(File file)
  391.         {
  392.             if(file != null)
  393.             {
  394.                 if (file.isDirectory())
  395.                 {
  396.                     String directory = file.getPath();
  397.                     if (!directory.endsWith("/"))
  398.                         directory += "/";
  399.     
  400.                     String[] fileList = file.list();
  401.     
  402.                     for (int fileInd = 0; fileInd < fileList.length; fileInd++)
  403.                         this.handleOpenFile(new File(directory + fileList[fileInd]));
  404.                 }
  405.                 else
  406.                 {
  407.                     String upperCaseName = file.getName().toUpperCase();
  408.                     if (upperCaseName.endsWith(".JPG") ||
  409.                         upperCaseName.endsWith(".JPEG") ||
  410.                         upperCaseName.endsWith(".GIF"))
  411.                     {
  412.                         files.addElement(file);
  413.                     }
  414.                 }
  415.             }
  416.         }
  417.         
  418.         /**
  419.          * This gets called when the application receives a quit event.
  420.          */
  421.         public void handleQuit()
  422.         {
  423.             doOnQuit();
  424.         }
  425.         
  426.         /**
  427.          * This gets called when the About menu item in the Apple Menu is selected.
  428.          */
  429.         public void handleAbout()
  430.         {
  431.             doAbout();
  432.         }
  433.     }    
  434.  
  435.     //Inner class for handling ActionEvents
  436.     //Insert "SlideShow Action"
  437.     class Action implements ActionListener
  438.     {
  439.         public void actionPerformed(ActionEvent event)
  440.         {
  441.             Object object = event.getSource();
  442.             if (object == openMenuItem)
  443.                 openMenuItem_ActionPerformed(event);
  444.             else if (object == quitMenuItem)
  445.                 quitMenuItem_ActionEvent(event);
  446.             else if (object == controlsMenuItem)
  447.                 controlsMenuItem_ActionPerformed(event);
  448.             else if (object == fullScreenMenuItem)
  449.                 fullScreenMenuItem_ActionPerformed(event);
  450.             else if (object == playMenuItem)
  451.                 playMenuItem_ActionPerformed(event);
  452.             else if (object == backMenuItem)
  453.                 backMenuItem_ActionPerformed(event);
  454.             else if (object == forwardMenuItem)
  455.                 forwardMenuItem_ActionPerformed(event);
  456.             else if (object == controls)
  457.                 controls_ActionPerformed(event);
  458.         }
  459.     }
  460.  
  461.     //Routines for handling the various ActionEvents
  462.     //Insert "SlideShow Action Management"
  463.     void openMenuItem_ActionPerformed(ActionEvent event)
  464.     {
  465.         //Present the load file dialog.
  466.         openFileDialog1.setVisible(true);
  467.         
  468.         //Make sure a valid value is returned (user could cancel).
  469.         String resultPath = openFileDialog1.getDirectory();
  470.         String resultFile = openFileDialog1.getFile();
  471.         if(resultPath != null && resultPath.length() != 0 && resultFile != null && resultFile.length() != 0)
  472.         {
  473.             //Construct a File object from the information from the dialog.
  474.             File file = new File(resultPath + resultFile);
  475.             if(file != null)
  476.             {
  477.                 //Add the file to our list of image files.
  478.                 files.addElement(file);
  479.                 
  480.                 //Load the image from the file, and display it as our current image.
  481.                 Image image = Misc.loadImage(file.getPath(), this, false);
  482.                 if (image != null)
  483.                 {
  484.                     if (currentImage != null)
  485.                         currentImage.flush();
  486.                     currentImage = image;
  487.                     currentIndex = files.size() - 1;                    
  488.                     repaint();
  489.                 }
  490.             }
  491.         }
  492.     }
  493.  
  494.     void quitMenuItem_ActionEvent(ActionEvent event)
  495.     {
  496.         doOnQuit();
  497.     }
  498.  
  499.     void controlsMenuItem_ActionPerformed(ActionEvent event)
  500.     {
  501.         toggleControls();
  502.     }
  503.  
  504.     void fullScreenMenuItem_ActionPerformed(ActionEvent event)
  505.     {
  506.         toggleFullScreen();
  507.     }
  508.  
  509.     void playMenuItem_ActionPerformed(ActionEvent event)
  510.     {
  511.         togglePlaying();
  512.         controls.setPlayState(!isPlaying);
  513.     }
  514.  
  515.     void backMenuItem_ActionPerformed(ActionEvent event)
  516.     {
  517.         oneStep(false);
  518.     }
  519.  
  520.     void forwardMenuItem_ActionPerformed(ActionEvent event)
  521.     {
  522.         oneStep(true);
  523.     }
  524.     
  525.     void controls_ActionPerformed(ActionEvent event)
  526.     {
  527.         String command = event.getActionCommand();
  528.         
  529.         if (command.equals(Controller.BACKWARD_COMMAND))
  530.             oneStep(false);
  531.         else if (command.equals(Controller.FORWARD_COMMAND))
  532.             oneStep(true);
  533.         else if (command.equals(Controller.PLAY_COMMAND))
  534.             togglePlaying();
  535.         else if (command.equals(Controller.PAUSE_COMMAND))
  536.             togglePlaying();
  537.     }
  538. }
  539.